package com.xhwl.logistics.controller.logistics;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.Organization;
import com.xhwl.logistics.entity.logistics.LogisticTag;
import com.xhwl.logistics.enums.logistics.TagTypeEnmu;
import com.xhwl.logistics.logistics.request.CrenelGroupParam;
import com.xhwl.logistics.query.CrenelGroupQuery;
import com.xhwl.logistics.query.CrenelTagQuery;
import com.xhwl.logistics.service.logistics.CrenelGroupService;
import com.xhwl.logistics.service.logistics.ILogisticTagService;
import com.xhwl.logistics.util.PageResult;
import com.xhwl.logistics.vo.logistics.CrenelGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Api(tags = "垛口分组管理（垛口管理）")
@RequestMapping(value = "crenel")
@Slf4j
@RestController
public class CrenelController {

    @Autowired
    private CrenelGroupService crenelGroupService;


    /**
     * 垛口标记
     */
    @Autowired
    private ILogisticTagService logisticTagService;

    @ApiOperation(value = "垛口分页管理")
    @PostMapping(value = "pageCrenel")
    public ResultJson<PageResult<CrenelGroupVO>> pageCrenel(@RequestBody CrenelGroupQuery crenelGroupQuery){
        log.info("pageCrenel() called with parameters => 【crenelGroupQuery = {}】",crenelGroupQuery);
        PageResult<CrenelGroupVO> crenelGroupVOPageResult = crenelGroupService.pageCrenelGroup(crenelGroupQuery);
        return ResultJson.success(crenelGroupVOPageResult);
    }


    @ApiOperation(value = "保存垛口分组")
    @PostMapping(value = "saveOrUpdateCrenelGroup")
    public ResultJson saveOrUpdateCrenelGroup(@Valid @RequestBody CrenelGroupParam params, Account account){
        log.info("saveOrUpdateCrenelGroup() called with parameters => 【params = {}】", JSON.toJSONString(params));
        String accountName = Optional.ofNullable(account).map(x -> x.getName()).orElse(null);
        if (ObjectUtil.isNotNull(accountName)) {
            params.setCreateUser(accountName);
        }
        return this.crenelGroupService.saveOrUpdateCrenelGroup(params);
    }

    @PostMapping(value = "queryCrenelGroupList")
    @ApiOperation(value = "获取垛口分组列表或者详情")
    public ResultJson<CrenelGroupVO> queryCrenelGroupList(@RequestBody CrenelGroupQuery query){
        log.info("queryCrenelGroupList() called with parameters => 【query = {}】",query);
        return this.crenelGroupService.queryCrenelGroupList(query);
    }

    @ApiOperation(value = "垛口分组删除")
    @PostMapping(value = "deleteCrenelByIds")
    public ResultJson deleteCrenelByIds(String ids){
        log.info("deleteCrenelByIds() called with parameters => 【ids = {}】",ids);
        return this.crenelGroupService.deleteCrenelGroupService(ids);
    }

    @ApiOperation(value = "通过分组id获取设备列表信息")
    @PostMapping(value = "queryDeviceByGroupIds")
    public ResultJson queryDeviceBcoyGroupIds(String ids){
        log.info("queryDeviceByGroupIds() called with parameters => 【ids = {}】",ids);
        return this.crenelGroupService.queryDeviceByGroupIds(ids);
    }

    @ApiOperation(value = "标记组织路址为垛口")
    @PostMapping(value = "tagOrganizationToLogistics")
    public ResultJson tagOrganizationToLogistics(@RequestBody List<Organization> organizationList){
        log.info("tagOrganizationToLogistics() called with parameters => 【organizationList = {}】",organizationList);
        if (organizationList.size() > 0) {
            List<LogisticTag> list = new ArrayList<>();
            try {
                for (Organization organization : organizationList) {
                    LogisticTag logisticTag = new LogisticTag();
                    logisticTag.setOrgId(organization.getId());
                    logisticTag.setOrgName(organization.getName());
                    logisticTag.setType(TagTypeEnmu.LOGISTICS.getCode());
                    list.add(logisticTag);
                }
                logisticTagService.saveOrUpdateBatch(list);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultJson.fail(e.getMessage());
            }
        }
        return ResultJson.success();
    }


    @ApiOperation(value = "查询垛口相关的组织节点")
    @PostMapping(value = "listTagLogistics")
    public ResultJson listTagLogistics(@RequestBody CrenelTagQuery query){
        log.info("listTagLogistics() called with parameters => 【query = {}】",query);
        List<LogisticTag> list = this.logisticTagService.list();
        return ResultJson.successWithResult(list);
    }
}
